package cn.caiyj.algorithm.sort;

import cn.caiyj.algorithm.util.BaseUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.odps.udf.CodecCheck;

/**
 * @Description:选择排序 找数组中最小的元素，将他与第一个元素交换，如此反复
 * @author:CAIYJ
 * @Date:2018/10/24 21:30
 */
public class Selection {

    public static void sort(Comparable[] a, boolean print) {
        int n = a.length;
        for (int i = 0; i < n; i++) {
            int min = i;
            for (int j = i + 1; j < n; j++) {
                if (BaseUtil.less(a[j], a[min])) {
                    min = j;
                }
            }
            if (print) {
                System.out.println(JSON.toJSONString(a));
            }
            BaseUtil.exch(a, i, min);
        }
    }
}
